/**
 * All right reserved@Raygo 2022~2030
 */

package com.raygo.jfxdemo;

import java.io.IOException;
import java.net.URL;

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;

import com.raygo.jfxdemo.view.PaginationUI;
import com.raygo.jfxdemo.view.TableViewUI;
import com.raygo.jfxdemo.view.TreeViewUI;

/**
 * 主界面JAVAFX程序
 *
 * @author Raygo
 * @since 2022年3月1日
 */
public class HomeStage extends Application {
    /**
     * 启动
     *
     * @param args 参数
     */
    public static void start(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getResource("/fxml/MainView.fxml"));
        Parent root;
        try {
            root = loader.load();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        Scene scene = new Scene(root, 900, 500);
        scene.getStylesheets().add(getResource("/css/jfoenix-components.css").toExternalForm());
        primaryStage.setTitle("Hello");
        primaryStage.setScene(scene);
        primaryStage.show();

        // 查找按钮
        Button btnTableView = (Button) root.lookup("#btnTableView");
        if (btnTableView != null) {
            btnTableView.setOnMouseClicked((event) -> {
                TableViewUI ui = new TableViewUI();
                ui.show();
            });
        }

        // 查找按钮
        Button btnTreeView = (Button) root.lookup("#btnTreeView");
        if (btnTreeView != null) {
            btnTreeView.setOnMouseClicked((event) -> {
                TreeViewUI ui = new TreeViewUI();
                ui.show();
            });
        }
        
        // 查找按钮
        Button btnPagination = (Button) root.lookup("#btnPagination");
        if (btnPagination != null) {
            btnPagination.setOnMouseClicked((event) -> {
                PaginationUI ui = new PaginationUI();
                ui.show();
            });
        }
    }

    /**
     * 获取资源URL
     *
     * @param path 路径
     * @return URL
     */
    protected URL getResource(String path) {
        String newPath = path.startsWith("/") ? path : "/" + path;
        URL url = getClass().getResource(newPath);
        if (url != null) {
            return url;
        }
        return getClass().getResource("/resources" + newPath);
    }
}
